package com.lineage.server.serverpackets;

import com.lineage.server.datatables.lock.BoardReading;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.templates.L1Board;
import java.util.ArrayList;

public class S_Board extends ServerBasePacket {
  private byte[] _byte;
  
  public S_Board(L1NpcInstance npc) {
    this._byte = null;
    buildPacket(npc, 0);
  }
  
  public S_Board(L1NpcInstance npc, int number) {
    this._byte = null;
    buildPacket(npc, number);
  }
  
  private void buildPacket(L1NpcInstance npc, int number) {
    int count = 0;
    ArrayList<L1Board> showList = new ArrayList<>();
    int maxid = BoardReading.get().getMaxId();
    while (count < 8 && maxid > 0) {
      L1Board boardInfo = BoardReading.get().getBoardTable(maxid--);
      if (boardInfo != null && (boardInfo.get_id() <= number || number == 0)) {
        showList.add(count, boardInfo);
        count++;
      } 
    } 
    writeC(68);
    writeC(0);
    writeD(npc.getId());
    writeC(255);
    writeC(255);
    writeC(255);
    writeC(127);
    writeH(showList.size());
    writeH(300);
    int i = 0;
    while (i < showList.size()) {
      L1Board boardInfo2 = showList.get(i);
      if (boardInfo2 != null) {
        writeD(boardInfo2.get_id());
        writeS(boardInfo2.get_name());
        writeS(boardInfo2.get_date());
        writeS(boardInfo2.get_title());
      } 
      i++;
    } 
  }
  
  public S_Board(int objectid) {
    this._byte = null;
    writeC(68);
    writeC(0);
    writeD(objectid);
    writeC(255);
    writeC(255);
    writeC(255);
    writeC(127);
    writeH(2);
    writeH(300);
    writeD(2);
    writeS("布告栏列表");
    writeS("2010-2-2");
    writeS("布告栏列表TITLE");
    writeD(1);
    writeS("布告栏列表2");
    writeS("2010-2-3");
    writeS("布告栏列表TITLE2");
  }
  
  public byte[] getContent() {
    if (this._byte == null)
      this._byte = getBytes(); 
    return this._byte;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
